import data_control from "../../../engines/libs/data_control";
import level_PeopleArcheryUnitBase from "../base/level_PeopleArcheryUnitBase";
import { peopleArcheryUnitTag } from "../const/peopleArcheryConst";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_PeopleArcheryBullet extends level_PeopleArcheryUnitBase {

    moveDis:number = 0;

    maxDis:number = 0;

    @property({type:cc.Animation,tooltip:"子弹动画"})
    bulletAni:cc.Animation = null;

    setMaxDis(maxDis:number){
        this.maxDis = maxDis;
    }

    initBullet(){
        this.moveDis = 0;
        this.isDeath = false;
        this.node.getComponent(cc.Collider).enabled = true;
        cc.Tween.stopAllByTarget(this.node);
        this.node.opacity = 255;
    }

    onEnable(){
        switch(this.level.role.profile){
            case "0":{
                this.bulletAni.play("putong");
                break;
            }
            case "1":{
                this.bulletAni.play("xian");
                break;
            }
            case "2":{
                this.bulletAni.play("mo");
                break;
            }
        }
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider): void {
        switch(other.tag){
            case peopleArcheryUnitTag.enemy:{
                let os = other.node.getComponent(level_PeopleArcheryUnitBase);
                let criticalFlag:boolean = false;
                // if(!os.isDeath){
                    let atk :number = this.atk;
                    if(Math.random() < this.criticalPro){
                        // 暴击 计算暴击伤害
                        let addition:number = Math.floor(atk * this.criticalAddition);
                        atk += addition;
                        criticalFlag = true;
                    }

                    let shotLeech:number = this.shotLeech;
                    let hpRecover:number = 0;
                    if(shotLeech > 0){
                        //射击吸血 计算吸血量
                        hpRecover = atk * shotLeech;
                    }
                    if(hpRecover > 0){
                        let data = {param_type:"0",option_param:hpRecover,reCoverMaxHp:"0",additionFlag:false}
                        this.level.parseDataAddPeople(data);
                    }
                    os.unitBeHurt(atk,"0",criticalFlag);
                    this.unitDeath();
                    this.unitDestroy();
                // }
                break;
            }
        }
    }

    update(dt: number): void {
        // super.update(dt);
        if(this.level.gamePauseFlag || this.isDeath){
            return;
        }
        let curFrameMoveDis:number = this.moveSpd * dt;
        this.moveDis += curFrameMoveDis;
        this.node.y += curFrameMoveDis;
        // 超过移动距离
        if(this.moveDis >= this.maxDis){
            // 销毁
            this.unitDeath();
        }
    }

    unitDestroy(){
        this.level.bulletPool.put(this.node);
        this.node.removeFromParent(false);
    }


}
